<template>
  <div>
    <el-dialog
      :title="bugtitle"
      :visible.sync="dialogFormVisible"
      width="600px"
      center
      :show-close="false"
      :close-on-click-modal="false"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="一级Bug" prop="testBugOne">
          <!-- type 可输入e . 字符；故添加正则 -->
          <el-input
            v-model.number="ruleForm.testBugOne"
            type="number"
            oninput="value=value.replace(/[^\d]/g,'')"
          >
            <template slot="append">个</template>
          </el-input>
        </el-form-item>
        <el-form-item label="常规Bug" prop="testBugCount">
          <el-input
            v-model.number="ruleForm.testBugCount"
            type="number"
            oninput="value=value.replace(/[^\d]/g,'')"
          >
            <template slot="append">个</template>
          </el-input>
        </el-form-item>
        <!-- <el-form-item class="footer-btn">
          <el-button @click="resetForm('ruleForm')">取 消</el-button>
          <el-button type="primary" @click="submitbug('ruleForm')">确 定</el-button>
        </el-form-item>-->
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="resetForm('ruleForm')">取 消</el-button>
        <el-button type="primary" @click="submitbug('ruleForm')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { postsubmitDevUserBugs } from "@/service/progressApi";
export default {
  name: "bugManageDialog",
  data() {
    return {
      dialogFormVisible: false,
      verBugId: "",
      bugtitle: "",
      devUserId: "", //开发人员id
      type: "", //新增或删减
      ruleForm: {
        testBugOne: "",
        testBugCount: ""
      },
      rules: {
        testBugOne: [
          { required: true, message: "请输入一级Bug数量", trigger: "blur" }
        ],
        testBugCount: [
          { required: true, message: "请输入Bug总数", trigger: "blur" }
        ]
      },
      bugObj: {}
    };
  },
  methods: {
    handleAddbug(row, title) {
      console.log("rowrow", row);
      //   用于限制减除bug，不能减除大于已有bug数，造成已有bug数为负数的情况
      this.bugObj = {
        bugLevelOne: row.bugLevelOne, //一级bug
        bugRuleCount: row.bugRuleCount //常规bug
      };
      this.bugtitle = title;
      this.dialogFormVisible = true;
      this.verBugId = row.id;
      this.devUserId = row.devUserId;
    },
    submitbug(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          if (this.bugtitle === "新增bug") {
            this.type = 0;
          } else {
            this.type = 1;

            // 减除bug数超出现有bug数 情况
            if (this.ruleForm.testBugOne > this.bugObj.bugLevelOne) {
              this.$message.warning(
                "您输入的减除一级bug数大于现有一级bug数！请确认重新输入~"
              );
              return;
            }
            if (this.ruleForm.testBugCount > this.bugObj.bugRuleCount) {
              this.$message.warning(
                "您输入的减除常规bug数大于现有常规bug数！请确认重新输入~"
              );
              return;
            }
          }
          postsubmitDevUserBugs(this.type, {
            verBugId: this.verBugId,
            devUserId: this.devUserId,
            testBugCount: this.ruleForm.testBugCount,
            testBugOne: this.ruleForm.testBugOne
          }).then(res => {
            this.$message.success(this.type === 0 ? "新增成功~" : "删减成功~");
            this.dialogFormVisible = false;
            this.$emit("refreshBug");
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.dialogFormVisible = false;
      this.$refs[formName].resetFields();
    }
  }
};
</script>

<style scoped>
</style>